package fr.nantes.univ.DAO;

import java.util.List;

import javax.persistence.EntityManager;

import fr.nantes.univ.EMF;
import fr.nantes.univ.EntityBeans.RubbishCollection;

public class RubbishCollectionDAO {
	
	private static EntityManager instance = EMF.get().createEntityManager();

	public static RubbishCollection findById(String rivoli) {
		RubbishCollection rb = instance
				.createQuery("select r from RubbishCollection r where r.rivoli = :rivoli", RubbishCollection.class)
				.setParameter("rivoli", rivoli)
				.getSingleResult();
		return rb;
	}
	
	public static List<RubbishCollection> findAll() {
		List<RubbishCollection> rcs = instance
				.createQuery("select r from RubbishCollection r", RubbishCollection.class)
				.getResultList();
		return rcs;
	}
	
	public static void insert(RubbishCollection rubbishCollection) {
		instance.getTransaction().begin();
		instance.persist(rubbishCollection);
		instance.getTransaction().commit();
	}
	
	public static void deleteAll() {
		instance.createQuery("DELETE FROM RubbishCollection a")
				.executeUpdate();
	}
	
}
